/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.bean.RelationBean;
import com.koron.risk.bean.SourceQueryBean;
import com.koron.risk.service.api.HazardSourceDistributeService;
import com.koron.risk.util.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "HazardSourceDistribute", description = "危险源分布")
@RestController
@RequestMapping("/HazardSourceDistribute")
public class HazardSourceDistributeController {

    private static final Logger logger = LoggerFactory.getLogger(HazardSourceDistributeController.class);
    @Autowired
    private HazardSourceDistributeService service;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @ApiOperation("危险源列表")
    @RequestMapping(value = "/list.htm", method = {RequestMethod.GET})
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SourceQueryBean queryBean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.list(queryBean);
    }

    @SuppressWarnings("rawtypes")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @ApiOperation("导出")
    @RequestMapping(value = "/exportListByGrid.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportListByGrid(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SourceQueryBean queryBean) {
        if (null == user) {
            return null;
        }
        MessageBean<?> msg = service.childAllByGrid(queryBean);
        if (msg.getData() == null) {
            return null;
        }
        File targetFile = new File("static/excel/LocalHazardSource.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/LocalHazardSource.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        DataBean data = (DataBean) msg.getData();
        Map<String, Object> map = new HashMap<>();
        map.put("data", data.getList());
        return Excel.createFile("部门危险源台账", targetFile, "LocalHazardSource.btl", map);
    }


    @SuppressWarnings("rawtypes")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @ApiOperation("导出")
    @RequestMapping(value = "/exportList.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SourceQueryBean queryBean) {
        if (null == user) {
            return null;
        }
        MessageBean<?> msg = service.childAll(queryBean);
        if (msg.getData() == null) {
            return null;
        }
        File targetFile = new File("static/excel/HazardSourceDistribute.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/HazardSourceDistribute.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        DataBean data = (DataBean) msg.getData();
        Map<String, Object> map = new HashMap<>();
        map.put("data", data.getList());
        return Excel.createFile("公司危险源分布", targetFile, "HazardSourceDistribute.btl", map);
    }

    @ApiOperation("删除关联危险源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lhscode", value = "危险源code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteRelation.htm", method = {RequestMethod.POST})
    @Log(title = "删除关联危险源",businessModule = "危险源", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> deleteRelation(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String lhscode, String gridCode, String corpCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.delete(lhscode, gridCode, corpCode);
    }

    @ApiOperation("关联危险源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/addRelation.htm", method = {RequestMethod.POST})
    @Log(title = "关联危险源",businessModule = "危险源", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> addRelation(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody List<RelationBean> relationBeanList, String gridCode, String corpCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.insert(user, relationBeanList, gridCode, corpCode);
    }

    @ApiOperation("修改危险源数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "危险源code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardNumber", value = "数量" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "managerCode", value = "负责人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "managerName", value = "负责人名称" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/updateNum.htm", method = {RequestMethod.POST})
    @Log(title = "修改危险源数量",businessModule = "危险源", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> updateNum(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String code, String gridCode, int hazardNumber, String managerCode, String managerName) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.updateNum(code, gridCode, hazardNumber, managerCode, managerName, user);
    }

    @ApiOperation("查看二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "危险源code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/MatrixToImage.htm", method = {RequestMethod.GET})
    public MessageBean<?> MatrixToImage(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String code, String gridCode, String corpCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.MatrixToImage(code, gridCode, corpCode);
    }

    @ApiOperation("危险源列表(包含下级)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/childAll.htm", method = {RequestMethod.GET})
    public MessageBean<?> childAll(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SourceQueryBean queryBean) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.childAll(queryBean);
    }

    @ApiOperation("部门危险源台账(包含下级)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/childAllByGrid.htm", method = {RequestMethod.GET})
    public MessageBean<?> childAllByGrid(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SourceQueryBean queryBean) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.childAllByGrid(queryBean);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gridCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hazardTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "important", value = "重大危险源：0否,1是" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "exclusive", value = "专属危险源：0否,1是" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "disposalState", value = "专有危险源是否处理，扩充到标准库,0:未处理,1:已导出,2:已处理" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @ApiOperation("当前网格未关联的危险源列表")
    @RequestMapping(value = "/irrelevantList.htm", method = {RequestMethod.GET})
    public MessageBean<?> irrelevantList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SourceQueryBean queryBean) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.irrelevantList(queryBean);
    }
}
